//
//  AudioSetViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/2/27.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class AudioSetViewController: BaseTRTCViewController {
    var list:[String] = ["检查扬声器","输出级别","输出音量","检查麦克风","输出级别","输出音量","其他"]
    var uCusViews:[CuSetView] = []
    var uObtns:[NSButton] = []
    var isSpeaking:Bool = false
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, kSet_BasicWidth, 700))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        TRTCCloud.sharedInstance()?.startMicDeviceTest(300, testEcho: { (volume) in
            self.levelView2.level = Int(volume/10)
        })
    }
    
    func setupUI() {
        self.view.layer?.backgroundColor = color_gray(233).cgColor
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        containt.snp.makeConstraints { (make) in
            make.width.top.centerX.equalToSuperview()
            make.height.equalTo(kSetWinHeight+100)
        }
        //初始化子类
        self.uCusViews = self.initViews(inView: self.containt)
    }
    
    lazy var scrollView: NSScrollView = {
        let view = NSScrollView()
        view.automaticallyAdjustsContentInsets = true
        view.hasVerticalScroller = true
        view.backgroundColor = NSColor.clear
        view.contentView = self.clipView
        return view
    }()
    lazy var clipView: NSClipView = {
        let clipView = NSClipView.init(frame: CGRect.zero)
        clipView.documentView = self.containt;
        clipView.documentView?.enclosingScrollView?.scrollerInsets = NSEdgeInsetsMake(0, 0, 0, -30)
        return clipView
    }()
    
    lazy var containt: NSView = {
        let view = NSView()
        return view
    }()
    
    func getSpeakerDevicesList() -> [TRTCMediaDeviceInfo] {
        let list = TRTCCloud.sharedInstance()?.getSpeakerDevicesList()
        return list ?? []
    }
    
    func getMicDevicesList() -> [TRTCMediaDeviceInfo] {
        let list = TRTCCloud.sharedInstance()?.getMicDevicesList()
        return list ?? []
    }
    
    
    lazy var popBtn: NSPopUpButton = {
        let list = self.getSpeakerDevicesList()
        let deviceName = list.map { (info) -> String in
            return info.deviceName ?? "未知"
        }
        let current = TRTCCloud.sharedInstance()?.getCurrentSpeakerDevice()?.deviceName
        let btn = NSPopUpButton.init(title: current ?? "未知", target: self, action: #selector(popBtnAction(senber:)))
        btn.addItems(withTitles: deviceName)
        return btn
    }()
    lazy var popView: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var testSpeak: NSButton = {
        let btn = NSButton()
        btn.bezelStyle = .rounded
        btn.title = "开始测试"
        btn.font = NSFont.systemFont(ofSize: 14)
        btn.target = self
        btn.action = #selector(testSpeakAction(senber:))
        return btn
    }()
    
    
    
    lazy var popBtn2: NSPopUpButton = {
        let list = self.getMicDevicesList()
        let deviceName = list.map { (info) -> String in
            return info.deviceName ?? "未知"
        }
        let current = TRTCCloud.sharedInstance()?.getCurrentMicDevice()?.deviceName
        let btn = NSPopUpButton.init(title: current ?? "未知", target: self, action: #selector(popBtnAction(senber:)))
        btn.addItems(withTitles: deviceName)
        return btn
    }()
    
    lazy var oBtnView: NSView = {
        let view = NSView.init(frame: NSRect.zero)
        self.uObtns = self.initOBtns(inView: view)
        return view
    }()
    
    lazy var spView: SpeakerView = {
        let view = SpeakerView.init()
        return view
    }()
    
    lazy var spView2: SpeakerView = {
        let view = SpeakerView.init()
        return view
    }()
    
    func initOBtns(inView:NSView) -> [NSButton] {
        var uBtns:[NSButton] = []
        let btnTitles:[String] = ["持续噪声控制","间歇性噪声控制"]
        for (inx,val) in btnTitles.enumerated() {
            let btn = NSButton.init(title: val, target: self, action: #selector(oBtnAction(senber:)))
            btn.tag = 100+inx
            btn.setButtonType(NSButton.ButtonType.switch)
            btn.state = .on
            inView.addSubview(btn)
            uBtns.append(btn)
            btn.snp.makeConstraints { (make) in
                make.width.equalToSuperview()
                make.left.equalToSuperview()
                make.top.equalTo(inx*34)
                make.height.equalTo(24)
            }
        }
        return uBtns
    }
    
    lazy var levelView: VulamLevelView = {
        let view = VulamLevelView.init()
        view.level = 0
        return view
    }()
    
    lazy var levelView2: VulamLevelView = {
        let view = VulamLevelView.init()
        view.level = 0
        return view
    }()
    
    func initViews(inView:NSView) -> [CuSetView] {

        var uCusViews:[CuSetView] = []
        for (inx,val) in self.list.enumerated() {
            var cusV:NSView?
            var cusSize:CGSize = CGSize.init(width: 300, height: 26)
            switch inx {
            case 0:
                self.popView.addSubview(testSpeak)
                testSpeak.snp.makeConstraints { (make) in
                    make.width.equalTo(80)
                    make.height.equalTo(26)
                    make.right.equalToSuperview()
                    make.height.equalToSuperview()
                }
                
                self.popView.addSubview(popBtn)
                popBtn.snp.makeConstraints { (make) in
                    make.right.equalTo(testSpeak.snp.left).offset(-20)
                    make.height.equalTo(26)
                    make.left.equalToSuperview()
                    make.centerY.equalToSuperview()
                }
                cusV = self.popView
            case 1:
                cusV = self.levelView
            case 2:
                cusV = self.spView
                cusSize = CGSize.init(width: cusSize.width, height: 70)
            case 3:
                cusV = self.popBtn2
            case 4:
                cusV = self.levelView2
            case 5:
                cusV = self.spView2
                cusSize = CGSize.init(width: cusSize.width, height: 70)
                break
            case 6:
                cusV = self.oBtnView
                cusSize = CGSize.init(width: cusSize.width, height: 102)
            default:
                break
            }
            
            let cusView = CuSetView.init(text: val+" ", cusView: cusV, cusSize: cusSize)
            inView.addSubview(cusView)
            uCusViews.append(cusView)
            cusView.snp.makeConstraints { (make) in
                make.width.equalTo(cusSize.width+cusView.titleWidth+30)
                make.height.equalTo(cusSize.height)
                make.centerX.equalToSuperview().offset(-50)
                if inx == 0 {
                    make.top.equalTo(30)
                }else {
                    make.top.equalTo(uCusViews[inx-1].snp.bottom).offset(15)
                }
                
            }
        }
        return uCusViews
    }
    
    @objc func popBtnAction(senber:NSPopUpButton) {
        senber.title = senber.selectedItem!.title
    }
    
    @objc func oBtnAction(senber:NSPopUpButton) {
        
    }
    
    @objc func testSpeakAction(senber:NSButton) {
        self.isSpeaking = !self.isSpeaking
        let tx = isSpeaking ? "停止测试":"开始测试"
        senber.title = tx
        if self.isSpeaking {
            let path = Bundle.main.path(forResource: "test", ofType: "mp3")
            TRTCCloud.sharedInstance()?.startSpeakerDeviceTest(path, onVolumeChanged: { (Volnum, isok) in
                self.levelView.level = Int(Volnum/10)
            })
        }else {
            TRTCCloud.sharedInstance()?.stopSpeakerDeviceTest()
        }
        
    }
}
